Repository navigation
fix: normalize structured content attributes from third-party spans - #285
PabloPardoGarcia wants to merge 2 commits into
Conversation
The GenAI conventions now ask for gen_ai.input.messages, gen_ai.output.messages, gen_ai.system_instructions and gen_ai.tool.definitions as maps and arrays, which OpenTelemetry Python keeps on spans from 1.45. Every export pass read only strings, so a structured value skipped the media rebuild and its blob bytes could reach the backend. The exporter now writes such a value as its JSON text first, in the one encoding and uncut, and normalizes it exactly as that string. A blob part's byte content is written as its base64, so the part keeps its byte_size and loses its bytes; any other byte object is the marker.
Automated reviewThe design holds up. Converting to the one JSON text first and then running every existing pass on it is the right shape: the media rebuild and the strip stay in one place, and the One path does leak blob bytes, though, so this needs a fix before merge. 🔴 BlockerBlob bytes leak as base64 when the encoder refuses the value (
Repro (OTel 1.45, through blob = {"type": "blob", "mime_type": "image/png", "content": PNG}
cyc = [{"role": "user", "parts": [blob]}]; cyc[0]["self"] = cyc
# exported: "[{'role': 'user', 'parts': [{'type': 'blob', ..., 'content': 'iVBORw0KGgo...'
[{b"k": 1, "type": "blob", "content": PNG}] # same leak via a bytes keyCycles in message lists are unlikely, but the contract says bytes never leave, and this path breaks it silently. The existing tests miss it because Suggestion: only use the base64 form when it encodes. For example, in 🟡 SuggestionsSpan events carrying the same keys as structured values still leak a blob's base64 string ( The PR says events are unchanged and that byte objects inside maps become markers, which is true. But a structured blob whose span.add_event("gen_ai.client.inference.operation.details",
{"gen_ai.output.messages": [{"role": "assistant", "parts": [
{"type": "blob", "mime_type": "image/png", "content": B64}]}]})
# exported event attribute still contains 'iVBORw0KGgo...'This was already true before this PR, but it's the same class of gap the ticket closes for span attributes, and OTel 1.45 keeps maps on event attributes too. The fix is cheap: run Integral and small-exponent floats aren't byte-identical with TypeScript. The 💭 Nits
Good work on the mutation checks and on testing the floor job with a real-tracer skip. With the fallback fixed, this is ready from my side. |
…ncoded A blob part's bytes were written as base64 before encoding. When the encoder then refused the value (a cycle, a bytes or tuple key), the fallback was the repr of that converted value, where the base64 is a bare string the media rebuild and the strip do not read, so the image was exported. The base64 form is now kept only when the whole value encodes; otherwise the fallback is the text of the value as it came, whose byte objects are markers. Re-vendor sdk_cases with the lone byte object, float, data URL and nested blob cases.
|
Addressed in d7920b3:
|
|
Landed through #289, together with the rius-semconv v1.13.0 sync. |
semantic-conventions-genai #634 requires
gen_ai.input.messages,gen_ai.output.messages,gen_ai.system_instructionsandgen_ai.tool.definitionsto be structured values (maps and arrays) instead of JSON strings. OpenTelemetry Python keeps such values on a span from 1.45. Every export pass read only strings, so a structured value skipped the native media rebuild, and a blob's bytes could reach the backend.What changes
structured_content_as_json(normalization.py) is now the first pass ofNormalizingSpanExporter. For these four keys, a mapping, list or tuple is written as its JSON text. Every pass after it then handles the value exactly as it handles that string, so a structured value and its JSON string export the same attributes._serde.json_text, which sharesserialize's encoder but skips the finaltruncate.{"type": "blob", "content": b"..."}, as OTel 1.45 stores bytes) is written as base64, which is how a blob part's JSON carries it. The part is then rebuilt with itsbyte_sizeand loses its bytes. Any other byte object becomes the marker, as everywhere else in the SDK.MappingProxyType) are written as objects. A value the encoder refuses (a cycle, a bytes, tuple or datetime key) never fails the export. Its fallback is the safe repr of the value as it came, not of the base64-converted one, so a blob's bytes are a marker there too (the converted value's repr would carry the base64 as a bare string that neither the media rebuild nor the strip recognises). The base64 form is used only when the whole value encodes (_serde.strict_json_text). Strings, numbers, booleans and lone byte objects are not structured and are left alone. Other keys keep their structured values, as before.Shared cases
tests/fixtures/semconv/sdk_cases/structured_content.jsoncomes from glassflow/rius-semconv#113 and is vendored here from that branch's commit (de35796).test_sdk_cases.pyruns the cases. I'll re-sync frommainonce #113 merges. This PR's output is the reference for those expected results. The TypeScript SDK has a matching PR.Events are unchanged. Their attributes still go only through the strip, which already turns byte objects inside maps into markers.
Known follow-ups (not in this PR)
contentis already a base64 string passes through the strip unrecognised. This was true before this PR too; the fix is to runstructured_content_as_jsonon event attributes as well.1.0,1e-07and-0.0whereJSON.stringifywrites1,1e-7and0. This was already true ofserialize, and the shared cases deliberately hold no integral float and no exponent. The output matches TypeScript byte for byte for the shared cases, not for every float.Tests
tests/test_normalization_structured_content.py, written first and failing before the change. Review follow-up: a blob in a cyclic message list, beside a bytes key and beside a tuple key, throughNormalizingSpanExporter. All 3 failed (base64 exported) before the fallback fix. A test that goes through a real tracer skips on OTel versions that drop mapping attributes. The floor job (OTel 1.25) passes with that one skip.serializeinstead ofjson_textfails 3 tests (the cap test among them). Falling back to the converted value again fails the 3 new tests.ruff check,ruff format --check,mypyandpytest(2936 passed, 22 skipped, 1 xfailed) all pass locally.Closes RIUS-1874